﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VolumetricLines;
public class RayItem : MagicAttackExtentItem
{
   // GameObject laser;
   [HideInInspector]
    public float Width;
    [HideInInspector]
    public float Length;
    [HideInInspector]
    public Vector3 LocalOffset;
    public int Layer = 2;

    public RayItemField NewSetting;
    public override ItemField ItemFieldSetting { get => NewSetting; set => NewSetting = value as RayItemField; }
    public Color RayColor
    {
        set
        {
            this.GetComponentInChildren<VolumetricLineBehavior>().LineColor = value;
        }
        get
        {
            return this.GetComponentInChildren<VolumetricLineBehavior>().LineColor;
        }
    }
    public static List<Vector2> GetColliderPointsByBounds(Bounds bound)
    {
       return  UnsafeTool.GetRectBoundPoint(bound.center, bound.size).ConvertAll(s => (Vector2)s);
    }
    public Bounds RayBounds
    {
        set
        {
            _collider2D.points = UnsafeTool.GetRectBoundPoint(value.center,value.size).ConvertAll(s=> (Vector2)s).ToArray();
            Width = value.size.y;
            Length = value.size.x;
            LocalOffset =value.center;
     //       Debug.Log(value);
            if (value.size.y < 0)
            {
                Debug.Log("!!");
            }
            SetLaser();
        }
    }
    void SetLaser()
    {
        laser = this.GetComponentInChildren<VolumetricLineBehavior>();
        laser.transform.localPosition =LocalOffset;
        laser.LineWidth =Width * 4;
        laser.SetStartAndEndPoints(new Vector3(-Length / 2f, 0, 0), new Vector3( Length/ 2f, 0, 0));

    }
    public override void DownloadInstanceField(object args)
    {
        base.DownloadInstanceField(args);
        var bounds = UnsafeTool.GetBounds((args as RayItemField).TriggerColliderPoints.ConvertAll<Vector3>(s => s));
        Width = bounds.size.y;
        Length = bounds.size.x;
        LocalOffset = bounds.center;
   //     Debug.Log(GetField<RayItemField>());
        this.RayColor = new Color(GetField<RayItemField>().RayColor.x, GetField<RayItemField>().RayColor.y, GetField<RayItemField>().RayColor.z);
        SetLaser();
    }
    public override ItemField UploadInstanceField()
    {
        ItemFieldSetting=base.UploadInstanceField();
        var bounds = UnsafeTool.GetAreaRectBound((ItemFieldSetting as AttackExtentItemField).TriggerColliderPoints.ConvertAll<Vector2>(s => new Vector2(s.x, s.y)).ConvertAll(s=> (Vector3)s));
        (ItemFieldSetting as AttackExtentItemField).TriggerColliderPoints = new List<Vector2>(_collider2D.points);
        Color tp = this.RayColor;
        GetField<RayItemField>().RayColor =new Vector3(tp.r,tp.g,tp.b);
        return ItemFieldSetting;

    }
    protected VolumetricLineBehavior laser;
    public override void OnStart()
    {
        base.OnStart();
        this.GetComponentInChildren<MeshRenderer>().sortingOrder = Layer;
        //   var bounds = new Bounds(, );
        //    _collider2D.points = UnsafeTool.GetRectBoundPoint(LocalOffset, new Vector3(Length,Width, 0), 0f).ConvertAll<Vector2>(s=>s).ToArray();
        RayBounds = new Bounds(LocalOffset, new Vector3(Length, Width, 0));
    //    UploadInstanceField();
     //   DownloadInstanceField(this.ItemFieldSetting);
     //   Debug.Assert((ItemFieldSetting as AttackExtentItemField).TriggerColliderPoints.Count == 4);
        //bool isRotate;
        //if (v.x > v.y) v = new Vector3(v.y, v.x, v.z);


        /*
        laser = this.transform.Find("Laser").gameObject;
        
        
        List<Vector3> arr = new List<Vector3>();
     
        foreach(var x in (ItemFieldSetting as AttackExtentItemField).TriggerColliderPoints)
        {
            arr.Add(new Vector3(x.x,x.y,0)*10f);
        }
        //  arr.Reverse();
        laser.GetComponent<VolumetricLineStripBehavior>().LineWidth = 0.1f;
        laser.GetComponent<VolumetricLineStripBehavior>().UpdateLineVertices(arr.ToArray());*/
        //    lineRender.SetPositions(arr.ToArray());*/
    }
    public override void OnPreferenceDestory()
    {
     //   base.OnPreferenceDestory();
        //取消碎片效果
    }
}
[System.Serializable]
public class RayItemField:AttackExtentItemField
{
    [HideInInspector]
    public Vector3 RayColor;

}